package com.cbgolf.oa.user;


import com.cbgolf.oa.app.App;
import com.cbgolf.oa.util.Util;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;


/**
 * Created by seiya on 2016/11/8.
 */
public class UserData {

    private static DbManager db = x.getDb(App.getDaoConfig());
    private static User user;
    private static LoginUser loginUser;

    public static LoginUser getLoginUser() {
        if (null == loginUser) {
            try {
                List<LoginUser> list = db.findAll(LoginUser.class);
                loginUser = (Util.listIsNull(list) ? null : list.get(list.size() - 1));
                if (null == loginUser) {
                    db.delete(LoginUser.class);
                    loginUser = null;
                }
            } catch (DbException e) {
                e.printStackTrace();
            }
        }
        return loginUser;
    }

    public static List<LoginUser> getLoginUserList() {
        try {
            List<LoginUser> list = db.findAll(LoginUser.class);
            return list;
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void setLoginUser(LoginUser loginUser) {
        try {
            db.delete(LoginUser.class);
            if (null != loginUser) {
                try {
                    db.save(loginUser);
                } catch (DbException e1) {
                    db.dropTable(LoginUser.class);
                    db.save(loginUser);
                    e1.printStackTrace();
                }
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        UserData.loginUser = loginUser;
    }

    public static User getUser() {
        if (null == user) {
            try {
                List<User> list = db.findAll(User.class);
                user = (null == list || 0 == list.size() ? null : list.get(0));
                if (null == user || Util.isNull(user.getToken())) {
                    db.delete(User.class);
                    user = null;
                }
            } catch (DbException e) {
                e.printStackTrace();
            }
        }
        return user;
    }

    public static void setUser(User user) {
        try {
            db.delete(User.class);
            if (null != user) {
                try {
                    db.save(user);
                } catch (DbException e1) {
                    db.dropTable(User.class);
                    db.save(user);
                    e1.printStackTrace();
                }
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        UserData.user = user;
    }

    public static void upDataUser(User u) {
        try {
            if (null != u) {
//                u.setUserPwd(user.getUserPwd());
                u.setToken(null == getUser() ? "" : getUser().getToken());
                db.delete(User.class);
                try {
                    db.save(u);
                } catch (DbException e) {
                    db.dropTable(User.class);
                    db.save(u);
                    e.printStackTrace();
                }
                user = u;
            }
        } catch (DbException e) {
            e.printStackTrace();
        }

    }
}
